/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.picker;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.ResourceBundle;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;

/**
 * A component that lets you choose a time
 * 
 * @author Pascal
 */
public class TimeFieldPicker extends javax.swing.JPanel {

    private JPopupMenu popup;
    private TimePicker timePicker;
    
    private Calendar date;
    private SimpleDateFormat formatter;
    
    /**
     * Creates new form TimeFieldPicker
     */
    public TimeFieldPicker(Calendar date) {
        formatter = new SimpleDateFormat("HH:mm");
        
        initComponents();
        
        setDate(date, true);
        
        timePicker = new TimePicker(date);
        timePicker.setActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                timePickerDone(e);
            }
        });
        
        popup = new JPopupMenu();
        popup.add(timePicker);
    }
    
    public TimeFieldPicker() {
        this(Calendar.getInstance());
    }
    
    /**
     * Set the date
     * 
     * @param date the new date
     * @param updateText whether to update the textfield
     */
    public void setDate(Calendar date, boolean updateText) {
        if (date == null) {
            throw new NullPointerException();
        }
        
        this.date = date;
        
        if (updateText) {
            timeText.setText(formatter.format(date.getTime()));
        }
    }
    
    public Calendar getDate() {
        return date;
    }

    private void timePickerDone(ActionEvent e) {
        popup.setVisible(false);
        setDate(timePicker.getDate(), true);
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        timeText = new javax.swing.JTextField();
        selectTimeButton = new javax.swing.JButton();

        setLayout(new java.awt.GridBagLayout());

        timeText.setColumns(7);
        timeText.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        timeText.setText("11:30");
        timeText.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                timeTextFocusLost(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        add(timeText, gridBagConstraints);

        selectTimeButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/resources/time.png"))); // NOI18N
        selectTimeButton.setMinimumSize(new java.awt.Dimension(25, 22));
        selectTimeButton.setPreferredSize(new java.awt.Dimension(25, 22));
        selectTimeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectTimeButtonActionPerformed(evt);
            }
        });
        add(selectTimeButton, new java.awt.GridBagConstraints());
    }// </editor-fold>//GEN-END:initComponents

    private void selectTimeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectTimeButtonActionPerformed
        int x = selectTimeButton.getWidth() - (int)popup.getPreferredSize().getWidth();
        int y = selectTimeButton.getY() + selectTimeButton.getHeight();
        
        timePicker.setDate(date);
        
        popup.show(selectTimeButton, x, y);
    }//GEN-LAST:event_selectTimeButtonActionPerformed

    private void timeTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_timeTextFocusLost
        if (!timeText.getText().matches("([01]?[0-9]|2[0-3]):[0-5][0-9]")) {
            setDate(getDate(), true);
            
            JOptionPane.showMessageDialog(null,
                    ResourceBundle.getBundle("gui/guilabels").getString("timeformatwrong"),
                    ResourceBundle.getBundle("gui/guilabels").getString("error"),
                    JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_timeTextFocusLost

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton selectTimeButton;
    private javax.swing.JTextField timeText;
    // End of variables declaration//GEN-END:variables

}
